//
// Description: 5302. 加密解密字符串
// Created by Loading on 2022/4/3.
//

#include <bits/stdc++.h>

using namespace std;

class Encrypter {
public:
    Encrypter(vector<char> &keys, vector<string> &values, vector<string> &dictionary) {
        int n = keys.size();
        for (int i = 0; i < n; ++i) {
            _keys[keys[i]] = values[i];
        }

        /* 将dictionary的加密，为解密做准备 */
        for (const auto &s : dictionary) {
            cnt[encrypt(s)]++;
        }
    }

    string encrypt(string word1) {
        string res;
        for (auto c : word1) {
            res += _keys[c];
        }

        return res;
    }

    /* 逆向思维，直接返回dictionary的加密字符串中word2的个数即可 */
    /* 切勿使用模拟回溯，超时 */
    int decrypt(string word2) {
        return cnt[word2];
    }

private:
    // 存储key字符对应的value字符串
    unordered_map<char, string> _keys;
    // 存储将dictionary加密后的字符串及个数
    unordered_map<string, int> cnt;
};

int main() {
    vector<char> keys{'a', 'b', 'c', 'd'};
    vector<string> values{"ei", "zf", "ei", "am"};
    vector<string> dictionary{"abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"};
    string word1 = "abcd";
    string word2 = "eizfeiam";

    auto *obj = new Encrypter(keys, values, dictionary);
    string param_1 = obj->encrypt(word1);
    int param_2 = obj->decrypt(word2);

    cout << param_1 << endl;
    cout << param_2 << endl;

    return 0;
}